<?php
	session_start();

	if (!isset($_SESSION['login']) && !$_SESSION['login'] == true) {
		header ("Location: index.php");
	}
	include("LangHeader.php");
	include ("config.php");

	if(isset($_GET['ID']))
	{
		$id = $_GET['ID'];
	}
	else
	{
		header ("Location: listusers.php");
	}
	
	if(isset($_SESSION['SeeHidden']))
		$seeHidden = $_SESSION['SeeHidden'];
	else
	{
		$seeHidden = 0;
		$_SESSION['SeeHidden'] = 0;
	}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="scripts/jquery.tablesorter.js"></script>
<script src="scripts/salesmakeover.js"></script>
<title>Sales Makeover</title>
</head>

<body>
	<div class="container_head">
		<?php include("header.html"); ?>
	</div>
	<div class="container">
		<div class="editCompanyHeadline tr" key="edit_Label">Kontaktuppgifter</div>
		<div class="content_edit" id="content_edit">
			
			<?php
				$stmt = $con->prepare('SELECT ID, Name, Email FROM Company WHERE ID = ?');
				$stmt->bind_param('i', $id);
				$stmt->execute();
				$result = $stmt->get_result();
				$companyrow = $result->fetch_assoc();
				echo "<form class=\"form_editCompany\" style=\"height:135px;\" id=\"form_editCompany\" method=\"post\" action=\"saveCompany.php?ID=" . $companyrow["ID"] . "\" accept-charset=\"UTF-8\" onsubmit=\"SalesMakeover.disableBeforeUnload(); return SalesMakeover.validateFormSave(". $id . ");\">";
				
				echo "<div class=\"editCompanyLabel tr\" key=\"edit_CompanyName\">Företagets namn</div>";
				echo "<div style='width:1024px;height:50px;'><input style='float:left;' type='text' onchange=\"SalesMakeover.enableBeforeUnload();\" name='companyname' onblur='if(this.value == \"\") { this.value = \"" . $companyrow['Name'] . "\"; }' value=\"" . $companyrow['Name'] . "\">";
				
				echo "<span><div style='float:right;'><div class=\"button tr\" key=\"edit_AddManager\" onclick=\"SalesMakeover.hideDiv('form_editEmployee',false);\">Lägg till manager</div></div></span></div>";
				echo "<div class=\"editCompanyLabel tr\" key=\"edit_CompanyEmail\">Företagets e-post</div>";
				echo "<input style='float:left;' type='email' onchange=\"SalesMakeover.enableBeforeUnload();\" name='companyemail' onblur='if(this.value == \"\") { this.value = \"" . $companyrow['Email'] . "\"; }' value=\"" . $companyrow['Email'] . "\">";
				echo "<div style='float:right;' id=\"button\"><input type=\"submit\" class=\"button tr\" key=\"Save\" value=\"Spara\"></div>";
				echo "</form>";
				?>
				<span><div style='width:100%;height:30px;'><div style='float:right;'><label for='seeHidden' class='tr' key='See_Hidden'></label><input type='checkbox' id='seeHidden' name='seeHidden' onclick='SalesMakeover.clickHidden(this);' /></div></div></span>
				<table id="myTable" class="tablesorter" >
				<?php
				if($seeHidden == 1)
					$stmt = $con->prepare('SELECT User.ID, User.Name, User.Login, User.Active,User.ResetKey AS Activated, Company.Name AS CompanyName, User.Hidden FROM User INNER JOIN Company ON User.CompanyID = Company.ID WHERE User.CompanyID = ? AND User.UserRoleID = 2');
				else
					$stmt = $con->prepare('SELECT User.ID, User.Name, User.Login, User.Active,User.ResetKey AS Activated, Company.Name AS CompanyName, User.Hidden FROM User INNER JOIN Company ON User.CompanyID = Company.ID WHERE User.CompanyID = ? AND User.UserRoleID = 2 AND User.Hidden = 0');
				
				$stmt->bind_param('i', $id);
				$stmt->execute();
				$managersresult = $stmt->get_result();
				//$managersresult = mysqli_query($con,"SELECT User.ID, User.Name, User.Login, User.Active,User.ResetKey AS Activated, Company.Name AS CompanyName FROM User INNER JOIN Company ON User.CompanyID = Company.ID WHERE User.CompanyID = '$id' AND User.UserRoleID = 2");
				
				echo "<thead>";
				echo "<tr>";
				echo "<th class=\"name tr\" key=\"edit_Manager\" style='padding-left:15px;'>Managers</th>";
				echo "<th style='width: 170px;'></th>";
				echo "<th style='width: 360px;'></th>";
				
				echo "<th class=\"buttons tr\" key=\"Edit\">Redigera</th>";
				if($seeHidden == "1") 
					echo "<th class=\"buttons tr\" key=\"Activate\"></th>";
				else
					echo "<th class=\"buttons tr\" key=\"Activate\"></th>";
							
				echo "<th class=\"buttons tr\" key=\"Remove\">Ta bort</th>";
				echo "</tr>";
				echo "</thead>";
				echo "<tbody style='height: 200px;'id='tableHolder'>";

				while($row = $managersresult->fetch_assoc())
				{
					echo "<tr>";
					echo "<td class='name' style='padding-left:15px;'>";
					echo $row['Name'];
					echo "</td>";
					echo "<td class='business'>";
					echo $row['CompanyName'];
					echo "</td>";
					echo "<td class='email'>";
					echo $row['Login'];
					echo "</td>";
					echo "<td class='buttons'>";
					echo "<a href='#' ><div class='list_button_edit' onclick=\"javascript:location.href='editManager.php?ID=" . $id . "&mID=" . $row['ID'] . "'\" ></div></a>";
					echo "</td>";
					if($seeHidden == "1")
					{
						echo "<td class='buttons'>";
						if($row['Hidden'] == 1)
						{
							echo "<a href='#' ><div class='list_button_hidden' id=\"text1\" onMouseOver=\"show(getText('ButtonHidden'))\" onMouseOut=\"hide()\"  onclick='SalesMakeover.ReShowRow(" . $row['ID'] . ", \"" . $row['Name'] . "\")' ></div></a>";
						
						}
						echo "</td>";
					}
					else
					{
						if($row['Activated']!=null)
						{
							echo "<td class='buttons'>";
							echo "<div class='list_button_pending' id=\"text1\" onMouseOver=\"show(getText('ButtonYellow'))\" onMouseOut=\"hide()\" onclick=\"SalesMakeover.SendMail('" . $row['Login'] . "', '" . $row['Name'] . "', '" . $row['Activated'] . "')\" ><input type=\"hidden\" value=" . $row['Active'] . "></div>";
							echo "</td>";


						}
						else
						{
							switch($row['Active'])
							{
								case 0:
									echo "<td class='buttons'>";
									echo "<a href='#' ><div class='list_button_active'  id=\"text1\" onMouseOver=\"show(getText('ButtonRed'))\" onMouseOut=\"hide()\" onclick='SalesMakeover.ChangeActive(" . $row['ID'] . ")' ></div></a>";
									echo "</td>";
								break;
								case 1:
									echo "<td class='buttons'>";
									echo "<a href='#' ><div class='list_button_active_on' id=\"text1\" onMouseOver=\"show(getText('ButtonGreen'))\" onMouseOut=\"hide()\" onclick='SalesMakeover.ChangeActive(" . $row['ID'] . ")' ></div></a>";
									echo "</td>";
								break;
							}
						}
					}
					echo "<td class='buttons' >";
					if($row['Hidden'] == 0)
						echo "<a href='#' ><div class='list_button_remove' onclick='SalesMakeover.DeleteRow(" . $row['ID'] . ", \"" . $row['Name'] . "\")'></div></a>";
					echo "</td>";
					echo "</tr>";
					
				}
				echo "</tbody>";
				
			?>
			</table>
			

			<div class="OVER" id="form_editEmployee">
				<div class="overlay_editCompany_addmanager">
					<p class="overlay_text tr" key="edit_AddManager">Title</p><p class="overlay_textWhite"></p>
					<form id="editCompany_addManager" method="post" action="adding.php?id=manager" accept-charset="UTF-8" onsubmit="return SalesMakeover.validateFormNewManager();">
						<?php echo "<input type=\"hidden\" name=\"companyID\" value=\"". $id ."\">"; ?>
						<div class="editCompanyAddManagerLabel tr" key="editManager_Name" ></div>
						<input type="text" id="newmanagername" name="newmanagername">
						<br/>
						<div class="editCompanyAddManagerLabel tr" key="editManager_Email" ></div>
						<input type="email" id="newmanageremail" name="newmanageremail">
						<div class="form_add_buttons">
							<div class="form_add_left_align" id=\"button\" onclick="SalesMakeover.hideDiv('form_editEmployee',true)">
								<input  type="button" class="button_black tr" key="Exit">
							</div>
							<div class="form_add_right_align"style="margin-right: 9%;" id=\"button\">
								<input type="submit" class="button tr" key="Add">
							</div>
						</div>
					</form>
				</div>
			</div>
		
			<span><div class="button_black tr" key="Back" style="margin-top:10px;" onclick="javascript:location.href='listusers.php'"></div></span>
		
		</div>
	</div>
	<?php if(isset($_SESSION["Popup"]) && isset($_SESSION["MessageKey"])){ $message = $_SESSION['Popup'];$message2=$_SESSION['MessageKey']; echo "<script type='text/javascript'>setTimeout(function() { alert('$message '+getText('$message2')); }, 10.2);</script>"; unset($_SESSION['Popup']); unset($_SESSION['MessageKey']);} ?>
	
	<div class="Effect" id="ButtinInfo">Div 1 Content</div>

<footer>
<div class = 'WaterMark'>© 2014 Sales Makeover</div>
</footer>

</body>

</html>
<script type="text/javascript" charset="UTF-8">



var cursorX;
var cursorY;
document.onmousemove = function(e){
    cursorX = e.pageX;
    cursorY = e.pageY;
}
function show(text) 
{
	var d = document.getElementById('ButtinInfo');
	
	
	d.style.visibility = "visible";
	d.style.color = "gray";
	d.innerHTML = text;
	
	d.style.left = (cursorX-d.offsetWidth*0.5)+'px';
	d.style.top = (cursorY-d.offsetHeight-35)+'px';
}
  function hide()
  {
	var d = document.getElementById('ButtinInfo');
	d.style.visibility = "hidden";
  }

$(document).ready(function() 
{ 
	var showHidden = "<?php echo ( isset($_SESSION['SeeHidden']) && $_SESSION['SeeHidden'] != '') ? $_SESSION['SeeHidden'] : '';?>";
	if(showHidden == "1")
		$("#seeHidden").prop('checked', true);
	
	SalesMakeover.doResize();
	window.onresize = SalesMakeover.doResize;

       $("#myTable").tablesorter({
		widgets: ["zebra"],
		widgetOptions : {
			zebra : [ "even", "odd" ]
		}
	});

} ); 

var text = true;

function saveCompany(id)
{
	
var  formData = 'ID=' + id;
$.ajax({
	    url : "saveCompany.php",
	    type: "POST",
	    data : formData,
	    success: function(data)
	    {
			
	    },
	    error: function (jqXHR,textStatus,errorThrown)
	    {
		alert(textStatus);
	    }
	});
};

</script>

